#!/usr/bin/env python3
# coding=utf-8

# File Name: pushLib.py
# Author: Bill Zhang
# Mail: zchcandid@gmail.com 
# Created Time: Fri 25 Oct 2013 04:38:32 PM CST
#import urllib.parse
import urllib.request  
import hashlib
import json 
#mysql connector
import mysql.connector
from mysql.connector import errorcode

#get the SessionKey
def getSession(password):
    url="http://192.168.100.200:9998/Security/RegisterUser/json/T0030" 
    pwd=password  
    m = hashlib.md5(pwd.encode(encoding='utf-8'))  
    PassKey=m.hexdigest()
    #print(m.hexdigest())  
    print(PassKey)
    #data=urllib.parse.urlencode(password) 

    PassKey=PassKey.encode(encoding='utf-8', errors = 'strict')
    #创建请求对象  
    req=urllib.request.Request(url,PassKey) 
    #获得服务器返回的数据  
    response=urllib.request.urlopen(req) 
    sessionKey=response.read().decode('utf-8')
    SesKey=json.loads(sessionKey)
   # print(SesKey)
    print(SesKey["Result"])
    return SesKey["Result"]

#Manuplate the Database
def MysqlConnect(DataBase):
    try:
          cnx = mysql.connector.connect(user='admin001', password='admin001',host='127.0.0.1',database=DataBase)
    except mysql.connector.Error as err:
          if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
              print("Something is wrong with your user name or password")
          elif err.errno == errorcode.ER_BAD_DB_ERROR:
              print("Database does not exists")
          else:
              print(err)
    else:
        return cnx 
def pushing(URL,PushData):
    PushData=PushData.encode(encoding='utf-8', errors = 'strict')
    #创建请求对象  
    req=urllib.request.Request(URL,PushData) 
    #获得服务器返回的数据  
    response=urllib.request.urlopen(req) 
    id=response.read().decode('utf-8')
    ID=json.loads(id)
  
